﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    @RenderPage("CommonHead.cshtml")
    <title>@ViewBag.Title</title>
    @RenderSection("Head", required: false)

</head>
<body class="easyui-layout">
    <div data-options="region:'center',border:false" style="padding: 1px">
        <div class="easyui-layout" fit="true" border="false">
            <div region="center" border="false">
                <div id="grid">
                </div>
            </div>

            @RenderBody()
        </div>
    </div>
</body>
</html> 
 
<script type="text/javascript">
    // 全局变量
    var grid;
    var dlg_Edit;
    var dlg_Edit_form;
    var searchWin;
    var searchForm;
    var virpath = ""; //网站的虚拟目录 如：/ShopManager
    var config = {
 
        AddActionURL: '@ViewBag.AddActionURL',
        DelActionURL: '@ViewBag.DelActionURL',
        EditActionURL: '@ViewBag.EditActionURL',
        ViewActionURL: '@ViewBag.ViewActionURL',
        SelectActionURL:'@ViewBag.SelectActionURL',
        ListActionURL:'@ViewBag.ListActionURL',
        GridOption:{
     
            title: '@ViewBag.Title',
            iconCls: 'icon-save',
            methord: 'post',
            url: '@ViewBag.ListActionURL',
            method: 'post',
            sortName: '@ViewBag.ListSortName',
            sortOrder: '@ViewBag.ListSortOrder',
            idField: '@ViewBag.IdColumn',
            pageSize: 30,
            striped: true,
            queryParams:null,// { BookCategoryCode: 'Root' }
            frozenColumns: [[
                { field: 'ck', checkbox: true }

            ]],
            columns: null,
            fit: true,
            pagination: true,
            rownumbers: true,
            fitColumns: true,
            singleSelect: false,
            toolbar: [{
                text: '新增',
                iconCls: 'icon-add',
                handler: add
            }, '-', {
                text: '修改',
                iconCls: 'icon-edit',
                handler: edit
            }, '-', {
                text: '删除',
                iconCls: 'icon-remove',
                handler: del
            }, '-', {
                text: '查找',
                iconCls: 'icon-search',
                handler: OpensearchWin
            }], onDblClickRow: function (rowIndex, rowData) {  //双击事件
                edit();

            }, onHeaderContextMenu: function (e, field) {
                e.preventDefault();
                if (!$('#tmenu').length) {
                    createColumnMenu();
                }
                $('#tmenu').menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            }

        }
    };

    $.ajaxSetup({
        dataFilter:function(data, type) {
            if (typeof(data) == typeof('')) {
                //data = data.replace(new RegExp('(^|[^\\\\])\\"\\\\/Date\\((-?[0-9]+)\\)\\\\/\\"', 'g'), "$1new Date($2)");
                
            }
            return data ;
        }
    });
    $(document).ready(function () {
        


        grid = $('#grid').datagrid(config.GridOption);


 

        $('#btn-search,#btn-search-cancel').linkbutton();
        searchWin = $('#search-window').window({
            iconCls: 'icon-search',
            closed: true,
            modal: true
        });

        $("#tb .icon-add").click(function() {

            add();
        });  
           
        $("#tb .icon-edit").click(function() {

            edit();
        });   
        $("#tb .icon-remove").click(function() {
      
            del();

        });   
        searchForm = searchWin.find('form');

        $('body').layout();

    });


    function createColumnMenu() {
        var tmenu = $('<div id="tmenu" style="width:100px;"></div>').appendTo('body');
        var fields = grid.datagrid('getColumnFields');
        for (var i = 0; i < fields.length; i++) {
            $('<div iconCls="icon-ok"/>').html(fields[i]).appendTo(tmenu);
        }
        tmenu.menu({
            onClick: function (item) {
                if (item.iconCls == 'icon-ok') {
                    grid.datagrid('hideColumn', item.text);
                    tmenu.menu('setIcon', {
                        target: item.target,
                        iconCls: 'icon-empty'
                    });
                } else {
                    grid.datagrid('showColumn', item.text);
                    tmenu.menu('setIcon', {
                        target: item.target,
                        iconCls: 'icon-ok'
                    });
                }
            }
        });
    }

    function getSelectedArr() {
        var ids = [];
        var rows = grid.datagrid('getSelections');
        for (var i = 0; i < rows.length; i++) {
            ids.push(rows[i].@ViewBag.IdColumn);
        }
        return ids;
    }
    
    function getSelectedID() {
        var ids = getSelectedArr();
        return ids.join(',');
    }
    function arr2str(arr) {
        return arr.join(',');
    }


    var dialog;
    function add() {
        var url = virpath + config.AddActionURL;
        dialog=parent.openWindow({
            title:'@ViewBag.AddWindowTitle',
            url: url,
            height: 600,
            width: 800,
            maximizable:true,
            isIframe: true,
            onClose:function(){  
                       
            } ,
            toolbar: [
               {
                   text: '保存',
                   iconCls: "icon-save",
                   handler: function () {
                       dialog.find("iframe")[0].contentWindow.saveData(function() {
                           dialog.dialog("close");
                           grid.datagrid('reload');
                       });
                       //alert(dialog.find("iframe")[0].contentWindow.saveData);
                   }
               },
               '-',
               {
                   text: '关闭',
                   iconCls: "icon-no",
                   handler: function () {
                       dialog.dialog("close");
                   }
               }
            ]
        });
    }


    function EditItem(rowIndex) {
      
        stopPropagation();
        var rows = grid.datagrid('getRows');
        var row = rows[rowIndex];
        var url=virpath + config.EditActionURL + '?id=' + row.@ViewBag.IdColumn;
       
        dialog=parent. openWindow({
            title: "您正在修改" + row.@ViewBag.NameColumn,
            url: url,
            height: 600,
            width: 800,
            maximizable:true,
            isIframe: true ,
            toolbar:[
                {
                    text:'保存',
                    iconCls:"icon-save",
                    handler:function() {
                        dialog.find("iframe")[0].contentWindow.saveData(function(f) {
                            if (f) {
                                dialog.dialog("close");
                                grid.datagrid('reload');
                            }
                        });
                    }
                },   
                {
                    text:'关闭',
                    iconCls:"icon-no",
                    handler:function() {
                        dialog.dialog("close");
                    }
                }
            ]  ,
            onClose:function(){  
                         
            }
        });
    }
    function ViewItem(rowIndex) {
        stopPropagation();
        var rows = grid.datagrid('getRows');
        var row = rows[rowIndex];
        var url=virpath + config.ViewActionURL + '?id=' +row.@ViewBag.IdColumn;
       
        dialog=parent. openWindow({
            title: "您正在查看" + row.@ViewBag.NameColumn,
            url: url,
            height: 600,
            width: 800,
            maximizable:true,
            isIframe: true ,
            toolbar:[
                     
                {
                    text:'关闭',
                    iconCls:"icon-no",
                    handler:function() {
                        dialog.dialog("close");
                    }
                }
            ]  ,
            onClose:function(){  
                         
            }
        });
    }
    
    function edit() {
        var rows = grid.datagrid('getSelections');
       
        var num = rows.length;
        if (num == 0) {
            Msgshow('请选择一条记录进行操作!');
            //$.messager.alert('提示', '请选择一条记录进行操作!', 'question');       
            return;
        }
        else if (num > 1) {
            Msgshow('您选择了多条记录,只能选择一条记录进行修改!'); //$.messager.alert('提示', '您选择了多条记录,只能选择一条记录进行修改!', 'info');
            return;
        }
        else {
            var url=virpath + config.EditActionURL + '?id=' + rows[0].@ViewBag.IdColumn;
       
            dialog= parent.openWindow({
                title: "您正在修改" + rows[0].@ViewBag.NameColumn,
                url: url,
                height: 600,
                width: 800,
                isIframe: true ,
                maximizable:true,
                toolbar:[
                    {
                        text:'保存',
                        iconCls:"icon-save",
                        handler:function() {
                            dialog.find("iframe")[0].contentWindow.saveData(function(f) {
                                if (f) {
                                    dialog.dialog("close");
                                    grid.datagrid('reload');
                                }
                            });
                        }
                    },
                    '-',
                    {
                        text:'关闭',
                        iconCls:"icon-no",
                        handler:function() {
                            dialog.dialog("close");
                        }
                    }
                ]  ,
                onClose:function(){  
                         
                }
            });
        }
    }
    
    function DelItem(rowIndex) {
        stopPropagation();
        var rows = grid.datagrid('getRows');
        var row = rows[rowIndex];
        var url=virpath + config.DelActionURL + '?ids=' +row.@ViewBag.IdColumn;
        
        $.messager.confirm('提示信息', '您确认要删除选中的所有记录吗?', function (data) {
            if (data) {
                $.ajax({
                    url: url,
                    type: 'post',
                    error: function () {
                        Msgalert('错误', '删除失败!', 'error');
                        grid.datagrid('clearSelections');
                    },
                    success: function (result) {
                        var data = eval('(' + result + ')');
                        if (data.success) {
                          
                            grid.datagrid('reload');
                            grid.datagrid('clearSelections');
                        } else {
                            Msgalert('错误', data.msg, 'error');
                        }
                    }
                });
            }
        });
    }

    function del() {  
    var arr = getSelectedArr();
 
    if (arr.length > 0) {
        $.messager.confirm('提示信息', '您确认要删除选中的所有记录吗?', function (data) {
            if (data) {
                $.ajax({
                    url: virpath + config.DelActionURL + '?ids=' + arr2str(arr),
                    type: 'post',
                    error: function () {
                        Msgalert('错误', '删除失败!', 'error');
                        grid.datagrid('clearSelections');
                    },
                    success: function (Result) {
                        var data = eval('(' + Result + ')');
                        if (data.success) {
                            Msgfade(arr.length + "条记录" + data.msg); //提示消息
                            grid.datagrid('reload');
                            grid.datagrid('clearSelections');
                        } else {
                            Msgalert('错误', data.msg, 'error');
                        }
                    }
                });
            }
        });
    } else {
        Msgshow('请先选择要删除的记录。');

    }
}

 

function OpensearchWin() {
    searchWin.window('open');
    searchForm.form('clear');
}


function Close(parameters) {
    //alert($.fn)
}

function showAll() {
    grid.datagrid({ url: virpath + config.ListActionURL });
}
 
function SearchOK() {
      
    searchWin.window('close');
    var sf = searchForm.serializeJson();
    //alert(sf);
    grid.datagrid({ url: virpath + config.ListActionURL, queryParams: sf });
     
}
function closeSearchWindow() {
    searchWin.window('close');
}
    
</script>

@RenderSection("Script", required: false)

